package day0723;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class Test1 {
    public static void main(String[] args) {
        JFrame f = new JFrame("飞机大战");
        GamePane pane = new GamePane();
        f.add(pane);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(GamePane.width, GamePane.height);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        pane.startGame();
    }
}


class GamePane extends JPanel {

    static int height = 700;
    static int width = 420;

    ArrayList<FlyingObject> planes = new ArrayList<>();

    public void startGame() {
        while (true) {
            createFlyObjs();
            moveAction();
            repaint();
            try {
                Thread.sleep(110);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }




    private void moveAction() {
        for (int i = 0; i < planes.size(); i++) {
            FlyingObject fly = planes.get(i);
            fly.fly();
        }
    }




    int planeIndex=1;
    public void createFlyObjs() {
        if(planeIndex%10==0){
            Random r=new Random();
            int n=r.nextInt(20);
            if(0<=n&n<=10){
                Beeplane b1 = new Beeplane();
                planes.add(b1);
            }else if(10<n&n<=20){
                Airplane a1=new Airplane();
                planes.add(a1);
            }

        }
        planeIndex++;
    }





    public void paint(Graphics g) {//自动执行
        ImageIcon bgIcon = new ImageIcon("images1/background.png");
        g.drawImage(bgIcon.getImage(), 0, 0, null);
        for (int i = 0; i < planes.size(); i++) {
            FlyingObject fly = planes.get(i);
            g.drawImage(fly.beeImage, fly.x, fly.y,null);
            g.drawImage(fly.airplaneImage, fly.x, fly.y,null);
        }


    }


}







